package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.WorkLineDao;
import com.sls.nzdapplication.model.WorkLine;
import com.sls.nzdapplication.app.MySharedPreferences;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class WorkLineUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private WorkLine workLine;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public WorkLineUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertWorkLine(WorkLine workLine) {
        workLine.setCreateBy(username);
        manager.getDaoSession().insertOrReplace(workLine);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultWorkLine(final List<WorkLine> workLines) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (WorkLine workLine : workLines) {
                    workLine.setCreateBy(username);
                    manager.getDaoSession().insertOrReplace(workLine);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultWorkLine2(final List<WorkLine> workLines) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (WorkLine workLine : workLines) {
                    manager.getDaoSession().insertOrReplace(workLine);
                }
            }
        });
    }

    public WorkLine queryWorkLineByAreaName(String name){
        QueryBuilder<WorkLine> builder = manager.getDaoSession().queryBuilder(WorkLine.class);
        WorkLine workLine = builder.where(WorkLineDao.Properties.Name.eq(name)).unique();
        return  workLine;
    }

    /**
     * 删除数据
     */
    public void deleteWorkLine(WorkLine workLine) {
        manager.getDaoSession().delete(workLine);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateWorkLine(WorkLine workLine) {
        workLine.setModifyBy(username);
        workLine.setModifyTime(new Date());
        manager.getDaoSession().update(workLine);
    }

    /**
     * 按照主键返回单条数据
     */
    public WorkLine listOneWorkLine(String key) {
        return manager.getDaoSession().load(WorkLine.class, key);
    }


    /**
     * 查询全部数据
     */
    public List<WorkLine> queryAll() {
        return manager.getDaoSession().loadAll(WorkLine.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<WorkLine> WorkLines = queryAll();
        for(WorkLine WorkLine :WorkLines){
            jsonArray.add(WorkLine.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<WorkLine> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new WorkLine(jsonArray.getJSONObject(i)));
        }
        insertMultWorkLine2(list);
    }
}
